<?php

namespace app\admin\controller;

use think\Db;
class Huser extends AdminController
{
    public function index()
    {
        return view('admin@user/tables');
    }

    public function select()
    {


        //查询数据
            $list = Db::name('Huser')->select();
            
            // var_dump($list);die;
        
        return view('admin@Huser/index',['list'=>$list]);
    }

    public function add()
    {
        return view('admin@Huser/add');
    }

    public function doadd()
    {

//        dump(input('post.'));exit;
//        $p = $request->post();
        // $p = Request::instance()->post();
        // $p = input('post.');
//        var_dump($p);
        if(input('post.pwd') ==input('post.repwd') ) {

            $data = [
                'name' => input('post.name'),
                'email' => input('post.email'),
                'status' => input('post.status'),
                'userpass' => md5(input('post.pwd'))

            ];

       // var_dump($data);exit;
            $result = Db::name('Huser')->data($data)->insert();
//            dump($result);exit;
            if ($result > 0) {
                return $this->success('添加成功', url('admin/Huser/select'));
            } else {
                return $this->error('添加失败');
            }
        }else{return $this->error('两次密码不一致');}
    }

    public function del($id)
    {
//        dump($_GET);exit;
//        dump(input('get.id'));exit;
        $result = Db::name('Huser')->delete($id);
        if ($result > 0) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

    public function update($id)
    {
        $date = Db::name('Huser')->field('userpass', true)->find($id);
       // dump($date);exit;
        return view('admin@Huser/update',['date'=> $date]);
        
    }

    public function doupdate($id)
    {
//        dump(input('post.'));
        if(input('post.pwd') == null){
            $data = [
                'name' => input('post.name'),
                'status' => input('post.status')

            ];
        }else{
            $data = [
                'name' => input('post.name'),
                'status' => input('post.status'),
                'userpass' => md5(input('post.pwd'))
            ];
        }

       //        dump($data);exit;

        $result = Db::name('Huser')->where('id',$id)->update($data);

        if ($result) {
            return $this->success('编辑成功',url('admin/Huser/select'));
        } else {
            return $this->error('编辑失败');
        }
    }
   
}